import roleData from './role.js'
import userData from './user.js'
import routerData from './router.js'
import jwt from 'jsonwebtoken'
import fs from 'fs'
import path from 'path'
import Mock from 'mockjs'


export default [
    // mock模拟
    {
        url:'/list',
        method:'get',
        response(){
            let data=Mock.mock({
                'list|50':[
                    {
                        'id|1':+1,
                        name:'@cname'
                    }
                ]
            })
            return {
                data
            }
        }
    },
    // login
    {
        method:'post',
        url:'/getlogin',
        response(ctx){
            let {username,password}=ctx.body
            console.log(username,password)
            let user=userData.find(item=>item.username==username && item.password==password)
            let token=jwt.sign({
                username
            },'asdsfsdf')
            if(user){
                let role=roleData.find(item=>item.id==user.roleId)
                if(role.routerId=='*'){
                    return {
                        code:200,
                        msg:'登录成功',
                        username,
                        role:role.role,
                        routerData,
                        token
                    }
                }else{
                    return {
                        code:200,
                        msg:'登录成功',
                        username,
                        role:role.role,
                        routerData:routerData.filter(item=>role.routerId.includes(item.id)),
                        token
                    }
                }

            }else{
                ctx.body={
                    code:200,
                    msg:'登录失败'
                }
            }
        }
    },
    // 获取所有用户信息
    {
        url:'/getulist',
        mrthod:'get',
        response(){
            return{
                code:200,
                msg:'获取成功',
                data:userData
            }
        }
    },
    // 获取角色信息
    {
        url:'/getrole',
        method:'get',
        response(){
            const data=roleData.map(item=>{
                if(item.routerId=='*'){
                        item.tree=routerData
                }else{
                    item.tree=routerData.filter(val=>item.routerId.includes(val.id))
                }
                return item
            })
            console.log(data)
            return{
                code:200,
                msg:'获取成功',
                data:data
            }
        }
    },
    // 获取添加role信息
    {
        url:'/roleadd',
        method:"get",
        response(){
            return{
                code:200,
                msg:'获取成功',
                data:routerData
            }
        }
    },
    // role add
    {
        url:'/role/getadd',
        method:'post',
        response(ctx){
            let {name,auth}=ctx.body
            let respath=fs.readFileSync(path.join(__dirname, 'role.json'), 'utf-8')
            let resArr=JSON.parse(respath)
            resArr.push({
                id:+new Date(),
                role:name,
                routerId:auth.join(',')
            })
            fs.writeFileSync(path.join(__dirname,'role.json'),JSON.stringify(resArr))
            return {
                code:200,
                msg:'成功'
            }
        }
    },
    // role del
    {
        url:'/role/getdel/:id',
        method:'get',
        response(ctx){
            let {id}=ctx.query
            let respath=fs.readFileSync(path.join(__dirname,'role.json'),'utf-8')
            let resArr=JSON.parse(respath)
            resArr.forEach((item,index)=>{
                if(item.id==id){
                    resArr.splice(index, 1)
                }
            })
            fs.writeFileSync(path.join(__dirname,'role.json'),JSON.stringify(resArr))
            return {
                code:200,
                msg:'成功'
            }
        }
    },
    // edit获取修改信息
    {
        url:'/getedit/:id',
        method:'get',
        response(ctx){
            let {id}=ctx.query
            let data=roleData.find(item=>item.id==id)
            return {
                code:200,
                msg:'成功',
                data:data
            }
        }
    },
    //  role edit
    {
        url:'/role/getedit/:id',
        method:'post',
        response(ctx){
            let {id}=ctx.query
            let {name,auth}=ctx.body
              let respath=fs.readFileSync(path.join(__dirname,'role.json'),'utf-8')
            let resArr=JSON.parse(respath)
            let data=resArr.find(item=>item.id==id)
            data.role=name
            data.routerId=auth.join(',')
              fs.writeFileSync(path.join(__dirname,'role.json'),JSON.stringify(resArr))
            return {
                code:200,
                msg:'成功'
            }
        }
    },
    // auth 获取
    {
        url:'/getauth',
        method:'get',
        response(){
            return{
                code:200,
                msg:'获取成功',
                data:routerData
            }
        }
    },
    //auth add
    {
        url:'/getauthadd',
        methos:'post',
        response(ctx){
            let {path:mypath,name,pid}=ctx.body
            let respath=fs.readFileSync(path.join(__dirname,'router.json'),'utf-8')
            let resArr=JSON.parse(respath)
            resArr.push({
                id:+new Date(),
                path:mypath,
                name:name,
                pid:pid
            })
            fs.writeFileSync(path.join(__dirname,'router.json'),JSON.stringify(resArr))
            return{
                code:200,
                msg:'成功',
            }
        }
    },
    // auth del
    {
        url:'/authdel/:id',
        method:'get',
        response(ctx){
            let {id}=ctx.query
             let respath=fs.readFileSync(path.join(__dirname,'router.json'),'utf-8')
            let resArr=JSON.parse(respath)
            resArr.forEach((item,index)=>{
                if(item.id==id){
                    resArr.splice(index,1)
                }
            })
             fs.writeFileSync(path.join(__dirname,'router.json'),JSON.stringify(resArr))
               return{
                code:200,
                msg:'成功',
            }
        }
    },
    // auth edit 
    {
        url:'/authedit/:id',
        method:'post',
        response(ctx){
            let {id}=ctx.query
            let {path:mypath,name,pid}=ctx.body
            console.log(id,mypath,name,pid)
             let respath=fs.readFileSync(path.join(__dirname,'router.json'),'utf-8')
            let resArr=JSON.parse(respath)
            let data=resArr.find(item=>item.id==id)
            data.path=mypath
            data.name=name
            data.pid=pid
             fs.writeFileSync(path.join(__dirname,'router.json'),JSON.stringify(resArr))
             return{
                code:200,
                msg:'成功',
            }
        }
    }
];